/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import entities.Account;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author TungGreen
 */
@ManagedBean
@SessionScoped
public class LoginProgess {

    @EJB
    private biz.LoginProgess loginProgess;
    private String name;
    private String passUser;
    private String msg;
    private Account user = null;
    private Account user2 = null;
    private String fullName;
    private String link;
    private String hiden;
    private List<Account> listTechnical;
    private String image;

    /**
     * Function process login
     *
     * @return
     */
    public String doLoginProgess() {
        try {
            //call ejb to get Account login
            user = loginProgess.getUser(name);
            if (user.getPassWord().equals(passUser) && user.getRoleId().getId().equals(1)) {
                //return to admin page 
                user2 = user;
                fullName = user.getFullName();
                if (link == null) {
                    image = "admin.jpg";
                    return "admin_complain.xhtml";
                } else {
                    String temp = link;
                    link = null;
                    return temp;
                }
            }
            if (user.getPassWord().equals(passUser) && user.getRoleId().getId().equals(2)) {
                //return to technicain page
                fullName = user.getFullName();
                user2 = user;
                if (link == null) {
                    image = "technician.jpg";
                    return "technician_receive.xhtml";
                } else {
                    String temp = link;
                    link = null;
                    return temp;

                }
            } else {
                if (user.getPassWord().equals(passUser) && user.getRoleId().getId().equals(3)) {
                    //return to employee page
                    fullName = user.getFullName();
                    user2 = user;
                    if (link == null) {
                        image = "employee.jpg";
                        return "employee.xhtml";
                    } else {
                        String temp = link;
                        link = null;
                        return temp;
                    }
                } else {
                    msg = "Erorr Username or Password!";
                }
                return "";
            }
        } catch (Exception e) {
            msg = "Please input Username or Password!";
            return "";
        }
    }

    /**
     *
     * Function call list Technician
     *
     */
    public void doCreateListTechnical() {
        listTechnical = loginProgess.getListTechnical();

    }

    /**
     * Function Logout
     *
     * @return to index.xhtml
     */
    public String doLogout() {
        user2 = null;
        hiden = null;
        link = null;
        return "index.xhtml";
    }

    public String getCheckLogin() {
        if (user2 != null && user2.getRoleId().getId().equals(1)) {
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("admin.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
        if (user2 != null && user2.getRoleId().getId().equals(2)) {
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("technician.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
        if (user2 != null && user2.getRoleId().getId().equals(3)) {
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("employee.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
        return "";
    }

    /**
     * Function validate account in admin page
     *
     * @return
     */
    public String getCheckAdmin() {
        if (user2 != null && user2.getRoleId().getId().equals(1)) {
            return "";
        } else {
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    public String getCheckAdminComplain() {
        if (user2 != null && user2.getRoleId().getId().equals(1)) {
            return "";
        } else {
            link = "admin_complain.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    public String getCheckAdminRelation() {
        if (user2 != null && user2.getRoleId().getId().equals(1)) {
            return "";
        } else {
            link = "admin_relation.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    public String getCheckComlaintAdmin() {
        if (user2 != null && user2.getRoleId().getId().equals(1)) {
            return "";
        } else {
            link = "complain_admin.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    public String getCheckComlaintDetailEmployee() {
        if (user2 != null && user2.getRoleId().getId().equals(3)) {
            return "";
        } else {
            link = "complain_detail_employee.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    public String getCheckComlaintEmployee() {
        if (user2 != null && user2.getRoleId().getId().equals(3)) {
            return "";
        } else {
            link = "complain_employee.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    public String getCheckComlaintDetailAdmin() {
        if (user2 != null && user2.getRoleId().getId().equals(1)) {
            return "";
        } else {
            link = "complaint_detail_admin.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    public String getCheckComlaintDetailTechnician() {
        if (user2 != null && user2.getRoleId().getId().equals(2)) {
            return "";
        } else {
            link = "complaint_detail_technician.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    public String getCheckCreateComplaint() {
        if (user2 != null && user2.getRoleId().getId().equals(3)) {
            return "";
        } else {
            link = "create_complaint.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    public String getCheckCreateComplaintDetail() {
        if (user2 != null && user2.getRoleId().getId().equals(3)) {
            return "";
        } else {
            link = "create_complaint_detail.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    public String getCheckAdminRelationDetail() {
        if (user2 != null && user2.getRoleId().getId().equals(1)) {
            return "";
        } else {
            link = "admin_relation_detail.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    /**
     *
     * Function validate account in employee page
     *
     * @return
     */
    public String getCheckEmployee() {
        if (user2 != null && user2.getRoleId().getId().equals(3)) {
            return "";
        } else {
            link = "employee.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class.getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    /**
     *
     * Function validate account in technician page
     *
     * @return
     */
    public String getCheckTechnician() {
        if (user2 != null && user2.getRoleId().getId().equals(2)) {
            return "";
        } else {
            link = "technician.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    public String getCheckTechnicianReply() {
        if (user2 != null && user2.getRoleId().getId().equals(2)) {
            return "";
        } else {
            link = "technician_reply.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    public String getCheckTechnicianReceive() {
        if (user2 != null && user2.getRoleId().getId().equals(2)) {
            return "";
        } else {
            link = "technician_receive.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    /**
     *
     * Function validate account in create news page
     *
     * @return
     */
    public String getCheckCreateNews() {

        if (user2 != null && user2.getRoleId().getId().equals(2)) {
            return "";
        } else {
            link = "create_news.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    /**
     *
     * Function validate account in new_user.xhtml
     *
     * @return
     */
    public String getCheckNewUser() {

        if (user2 != null && user2.getRoleId().getId().equals(1)) {
            return "";
        } else {
            link = "new_user.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    /**
     *
     * Not use
     *
     * @return
     */
    /**
     *
     * Function validate account in create User page
     *
     * @return
     */
    public String getCheckCreateUser() {
        if (user2 != null && user2.getRoleId().getId().equals(1)) {
            return "";
        } else {
            link = "create_user.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    public String getCheckNewsDetailAdmin() {
        if (user2 != null && user2.getRoleId().getId().equals(1)) {
            return "";
        } else {
            link = "new_detail_admin.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    public String getCheckNewsDetailEmployee() {
        if (user2 != null && user2.getRoleId().getId().equals(3)) {
            return "";
        } else {
            link = "new_detail_employee.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    public String getCheckNewsDetailTechnician() {
        if (user2 != null && user2.getRoleId().getId().equals(2)) {
            return "";
        } else {
            link = "new_detail_technician.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    public String getCheckNewsAdmin() {
        if (user2 != null && user2.getRoleId().getId().equals(1)) {
            return "";
        } else {
            link = "news_admin.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    public String getCheckNewsTechnician() {
        if (user2 != null && user2.getRoleId().getId().equals(2)) {
            return "";
        } else {
            link = "news_technician.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    public String getCheckNewsEmployee() {
        if (user2 != null && user2.getRoleId().getId().equals(3)) {
            return "";
        } else {
            link = "news_employee.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    public String getCheckReplyDetail() {
        if (user2 != null && user2.getRoleId().getId().equals(2)) {
            return "";
        } else {
            link = "reply_detail.xhtml";
            try {
                ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).sendRedirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginProgess.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            return "";
        }
    }

    public Account getUser2() {
        return user2;
    }

    public void setUser2(Account user2) {
        this.user2 = user2;
    }

    public List<Account> getListTechnical() {
        return listTechnical;
    }

    public biz.LoginProgess getLoginProgess() {
        return loginProgess;
    }

    public void setLoginProgess(biz.LoginProgess loginProgess) {
        this.loginProgess = loginProgess;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassUser() {
        return passUser;
    }

    public void setPassUser(String passUser) {
        this.passUser = passUser;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Account getUser() {
        return user;
    }

    public void setUser(Account user) {
        this.user = user;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getHiden() {
        return hiden;
    }

    public void setHiden(String hiden) {
        this.hiden = hiden;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }
}
